package com.base.cn.platform.os.controller.mobile.course.play;

import com.base.cn.platform.os.common.MobileBaseController;
import com.base.cn.platform.os.common.utils.user.LoginForm;
import com.base.cn.platform.os.service.course.cou.play.PlayCourseService;
import com.base.cn.platform.os.service.manage.user.user.CusUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

/**
 * 课程播放Controller接口
 *
 * @author s.li
 * @create 2018-08-07-10:29
 */
@Controller
@RequestMapping("/mobile/uc/play")
public class MobileUserPlayController extends MobileBaseController {

    @Autowired
    private PlayCourseService playCourseService;
    @Autowired
    private CusUserService cusUserService;

    /**
     * 检查播放
     * @param buyCourseId  课程或套餐id(购买的课程ID)
     * @param courseId  课程ID
     * @param prevCatalogId 前一个目录ID
     * @param catalogId 章节id
     * @return 检查结果
     */
    @RequestMapping("/checkPlay")
    @ResponseBody
    public Map<String, Object> checkPlay(HttpServletRequest request,
                                         @RequestParam("buyCourseId") BigDecimal buyCourseId,
                                         @RequestParam("courseId") BigDecimal courseId,
                                         @RequestParam("prevCatalogId") BigDecimal prevCatalogId,
                                         @RequestParam("catalogId") BigDecimal catalogId) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String, Object> map = cusUserService.userInfo(userId, false, false, false, false, false, false, false);
        Integer wifi = (Integer) map.get("isWifi");
        Map<String, Object> objectMap = playCourseService.checkCourseCatalogPlay(buyCourseId, courseId, prevCatalogId, catalogId, userId);
        objectMap.put("wifi",wifi);
        return objectMap;
    }

    /**
     * 加载播放页面播放器
     * @param sign 签名
     * @return Map<String,Object>
     */
    @RequestMapping("/loadPlayData")
    @ResponseBody
    public Map<String, Object> loadPlayData(Model model,HttpServletRequest request,
                                            @RequestParam("sign") String sign){
        String nickname = webMobileLoginUtil.getNickname(request);
        String userAvatar = webMobileLoginUtil.getAvatar(request);
        BigDecimal loginUserId = webMobileLoginUtil.getLoginUserId(request);
        return  playCourseService.loadPlayData(sign, LoginForm.LOGIN_FROM_MOBILE,nickname,userAvatar,loginUserId);
    }
}
